package com.ruoyi.common.rabbitmq.utils;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.rabbitmq.domain.EmailNotifyTemplate;
import com.ruoyi.common.rabbitmq.domain.SmsNotifyTemplate;
import com.ruoyi.common.rabbitmq.domain.WxNotifyTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

public class NotifyUtils {

    public static EmailNotifyTemplate createEmailNotifyTemplate(Set<Object> notifyUsers, String content, String fromAlias, String subject){

        EmailNotifyTemplate emailNotifyTemplate = new EmailNotifyTemplate();

        List<Long> userIds = new LinkedList<>();
        List<String> userNames = new LinkedList<>();
        StringBuilder emailUrl = new StringBuilder();
        Set<String> emailSet = new HashSet<>();

        for(Object tmp : notifyUsers){
            Map user = JSON.parseObject(JSON.toJSONString(tmp), Map.class);
            if(user.get("email") == null){
                continue;
            }
            userIds.add(Long.parseLong(user.get("userId").toString()));
            userNames.add((String)user.get("nickName"));
            String email = (String)user.get("email");
            if(!emailSet.contains(email)){
                emailUrl.append((String)user.get("email"));
                emailUrl.append(",");
                emailSet.add(email);
            }

        }

        emailNotifyTemplate.setFromAlias(fromAlias);
        emailNotifyTemplate.setSubject(subject);
        emailNotifyTemplate.setNotifyUserId(userIds);
        emailNotifyTemplate.setNotifyUserName(userNames);
        emailNotifyTemplate.setTextBody(content);
        emailNotifyTemplate.setTargetEmails(emailUrl.toString());

        return emailNotifyTemplate;
    }




    public static SmsNotifyTemplate createIotSmsNotifyTemplate(Set<Object> notifyUsers, HashMap<String,String> paramMap, String templateCode){

        List<String> phones = new LinkedList<>();
        List<String> signNames = new LinkedList();
        List<HashMap<String,String>> paramMaps = new LinkedList<>();
        List<Long> userIds = new LinkedList<>();
        List<String> userNames = new LinkedList<>();

        for(Object tmp : notifyUsers){
            Map user = JSON.parseObject(JSON.toJSONString(tmp), Map.class);
            if(user.get("phonenumber") == null){
                continue;
            }
            phones.add((String)user.get("phonenumber"));
            signNames.add("智能物联");
            userIds.add(Long.parseLong(user.get("userId").toString()));
            userNames.add((String)user.get("userName"));
            paramMaps.add(paramMap);
        }

        SmsNotifyTemplate smsNotifyTemplate = new SmsNotifyTemplate();
        smsNotifyTemplate.setNotifyUserId(userIds);
        smsNotifyTemplate.setNotifyUserName(userNames);
        smsNotifyTemplate.setTemplateCode(templateCode);
        smsNotifyTemplate.setPhoneNumberJson(JSONObject.toJSONString(phones));
        smsNotifyTemplate.setSignNameJson(JSONObject.toJSONString(signNames));
        smsNotifyTemplate.setTemplateParamJson(JSONObject.toJSONString(paramMaps));

        return smsNotifyTemplate;
    }



    public static WxNotifyTemplate createWxNotifyTemplate(Set<Object> notifyUsers, HashMap<String,String> contentMap, String templateId, String page){

        List<String> toUsers = new LinkedList<>();

        List<Long> userIds = new LinkedList<>();
        List<String> userNames = new LinkedList<>();

        for(Object tmp : notifyUsers){
            Map user = JSON.parseObject(JSON.toJSONString(tmp), Map.class);
            if(user.get("wxOpenId") == null ){
                continue;
            }
            toUsers.add(user.get("wxOpenId").toString());
            userIds.add(Long.parseLong(user.get("userId").toString()));
            userNames.add((String)user.get("userName"));
        }

        WxNotifyTemplate wxNotifyTemplate = new WxNotifyTemplate();
        wxNotifyTemplate.setContentMap(contentMap);
        wxNotifyTemplate.setNotifyUserId(userIds);
        wxNotifyTemplate.setNotifyUserName(userNames);
        wxNotifyTemplate.setMiniprogramState("formal");
        wxNotifyTemplate.setPage(page);
        wxNotifyTemplate.setToUsers(toUsers);
        wxNotifyTemplate.setTemlateId(templateId);

        return wxNotifyTemplate;
    }



    public static String getNowDateString(){

        Date date = new Date();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = formater.format(date);

        return nowDate;
    }
}
